package br.com.arg.aplica.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.enterprise.inject.Produces;
import javax.faces.application.FacesMessage;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.arg.aplica.model.Atributo;
import br.com.arg.aplica.model.CasoDeUso;
import br.com.arg.aplica.model.Classe;
import br.com.arg.aplica.model.Sistema;
import br.com.arg.aplica.util.Utils;

// The @Stateful annotation eliminates the need for manual transaction demarcation
@Stateful
// The @Model stereotype is a convenience mechanism to make this a
// request-scoped bean that has an
// EL name
// Read more about the @Model stereotype in this FAQ:
// http://sfwk.org/Documentation/WhatIsThePurposeOfTheModelAnnotation
@Named 
@SessionScoped 
public class ClasseRegistration extends BaseRegistracao {

	@Inject
	private Event<Classe> classeEventSrc;
	
	@Inject 
	private List<Classe> classes;

	@Inject 
	private List<Sistema> sistemas;

	private Sistema theSistema;
	@Produces @Named
	public Sistema getTheSistema() {
		return theSistema;
	}

	private CasoDeUso theCasoDeUso;
	@Produces @Named
	public CasoDeUso getTheCasoDeUso() {
		return theCasoDeUso;
	}
	public void setTheCasoDeUso(CasoDeUso theCasoDeUso) {
		this.theCasoDeUso = theCasoDeUso;
		Utils.say("++++++++++++++> " + this.theCasoDeUso.getNome());
	}
	
	private Classe newClasse;
	@Produces	@Named
	public Classe getNewClasse() {
		return newClasse;
	}

	private Atributo newAtributo;
	@Produces @Named
	public Atributo getNewAtributo() {
		return newAtributo;
	}

//	Id do classe - usado para buscar classe	
	private String idClasse;
	public String getIdClasse() {
		return idClasse;
	}
	public void setIdClasse(String idClasse) {
		this.idClasse = idClasse;
	}

	//	Id do atributo - usado para buscar atributo	
	private String idAtributo;
	public String getIdAtributo() {
		return idAtributo;
	}
	public void setIdAtributo(String idAtributo) {
		this.idAtributo = idAtributo;
	}

//	Id do classe - usado para buscar classe	
	private String idSistema;
	public String getIdSistema() {
		return idSistema;
	}

	public void setIdSistema(String idSistema) {
		this.idSistema = idSistema;
	}

	public void sistemaFoiTrocado(){
		theSistema = em.find(Sistema.class, Long.parseLong(idSistema, 10));
		theCasoDeUso = (theSistema != null & theSistema.getCasosDeUso().size() > 0) ? theSistema.getCasosDeUso().get(0) : new CasoDeUso();
	}
	
	public void casoDeUsoFoiTrocado(){
		theCasoDeUso.setClasses(new ArrayList<Classe>());
		newClasse = new Classe();
		Utils.say("++++++++++++++++++> " + theCasoDeUso.getNome());
	}
	
	public void addClasse() {
		theCasoDeUso.getClasses().add(newClasse);
		System.out.println("+++++++++++++++++++ add class " + newClasse.getNome() + " cu " + theCasoDeUso.getNome());
		newClasse = new Classe();
		facesContext.addMessage(null, new FacesMessage(
				FacesMessage.SEVERITY_INFO, "Classe added",
				"Registration successful"));
	}
	
	public String registrar() throws Exception {
		addClasse();
		em.merge(theSistema);
		
		log.info(String.format("Registering aplica=%s", newClasse.getNome()));
		facesContext.addMessage(null, new FacesMessage(
				FacesMessage.SEVERITY_INFO, "Registered!",
				"Registration successful"));

		classeEventSrc.fire(newClasse);

		inicializa();
		
		return null;
	}

	
	
	public void novoClasse() {
		inicializa();
	}
	
	public void buscaClasse() {
		newClasse = em.find(Classe.class, Long.parseLong(idClasse, 10));
		System.out.println("++++++++++++++++++++++++>>> Buscando newClasse " + newClasse.getNome());
	}
	
	public void buscaAtributo() {
		newAtributo = em.find(Atributo.class, Long.parseLong(idAtributo, 10));
		System.out.println("++++++++++++++++++++++++>>> Buscando newAtributo " + newAtributo.getNome());
	}
	
	public String exclua() throws Exception {
		Classe apl = em.find(Classe.class, Long.parseLong(idClasse, 10));
		System.out.println("++++++++++++++++++++++++>>> Excluindo aplica " + apl.getNome());

		em.remove(apl);
		
		facesContext.addMessage(null, new FacesMessage(
				FacesMessage.SEVERITY_INFO, "Registered!",
				"Registration successful"));

		classeEventSrc.fire(newClasse);	

		return null;		
	}
	
	public void addAtributo() {
		newClasse.getAtributos().add(newAtributo);
		System.out.println("+++++++++++++++++++ add atributo " + newClasse.getNome() + " atributo " + newAtributo.getNome());
		newAtributo = new Atributo();
		facesContext.addMessage(null, new FacesMessage(
				FacesMessage.SEVERITY_INFO, "New UC added",
				"Registration successful"));
	}
	
	@PostConstruct
	public void inicializa() {
		newClasse = new Classe();
		newAtributo = new Atributo();
	}
}
